<?php
/**
*
* @author Blackpawn blackpawn666@hotmail.com - http://blackpawnsarchives.net
* @co_author GameCharmer admin@gamecharmer.com - http://gaming.gamecharmer.com
* @package Age of Dragons Mod :: ( Game Console Required )
* @version $Id: ranks.php 1.1.1 12-26-2009 Blackpawn
* @copyright (c) 2009 Blackpawns Archives
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/
/**
* @ignore
*/

define('GC_MOD', true);
define('IN_PHPBB', true);
define('AOD', true);

$phpbb_root_path =  '../../';
$templates_path = '../../../games/aod/templates/';
$phpEx = substr(strrchr(__FILE__, '.'), 1);

include($phpbb_root_path . 'common.' . $phpEx);
include($phpbb_root_path . 'includes/functions_display.' . $phpEx);
include($phpbb_root_path . 'includes/mods/game_console/gc_common.' . $phpEx);
include($phpbb_root_path . 'games/aod/common.' . $phpEx);

// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup();

// Set custom language path
$user->set_custom_lang_path($phpbb_root_path.'games/aod/language/');

//load our language file
$user->add_lang('common');

// Now add the phpBB language path
$user->set_custom_lang_path($phpbb_root_path . 'language');

// Load the Game Console language file
$user->add_lang('mods/game_console');

if ($user->data['user_id'] == ANONYMOUS)
{
	login_box('', $user->lang['LOGIN']);
}

if ($user->data['is_bot'])
{
	redirect(append_sid("{$phpbb_root_path}index.$phpEx"));
}

// Default Vars
$ranks_page = TRUE; 	// Highlights the ranks tab
$error_msgs = null;
$admin = 0;

// Var Requests
$cmd = request_var('cmd','');
$mode = request_var('mode', 0);
$start = request_var('start', 0);
$round = request_var('round', 0);
$pagination_url = append_sid("ranks.$phpEx?mode=".$mode."&round=$round");

// AOD Config
$ranks_per_page = $aod_config['ranks_per_page'];

// Gather some user info
$user_id = $user->data['user_id'];
$account_status = check_account_status($user_id);
if ($account_status == FALSE)
{
	redirect(append_sid("{$phpbb_root_path}/games/index.$phpEx"));
}

// Fetch kingdom data
$king = fetch_kingdom_data($user_id);
$admin_access = $king['admin_access'];
$kingdom_status = $king['kingdom_status'];

// Check to see if this user is a admin staff member
if ($user_id == 2 || $admin_access == 1)
{
	$admin = 1;
}

// Check kingdom status to see if this account is frozen
if($kingdom_status == 1)
{
	// Lets make sure the main admin can't be blocked
	if ($user_id <> 2){
		trigger_error($user->lang['FROZEN'] . back_link());
	}
}

// Build kingdoms List
$total_kingdoms = count_kingdoms();


switch($mode)
{
	case 1: // Land
	
		$mode_display = sprintF($user->lang['RANKS_LAND'],$ranks_per_page);
		$rank_display = $user->lang['LAND'];
		
		if($round > 1)
		{		
			$sql = "SELECT *
			FROM " . AOD_LAND_RANKS_TABLE . "
			WHERE round = $round
			ORDER BY amount DESC";

			$result = $db->sql_query_limit($sql, $ranks_per_page, $start);
			while ($row = $db->sql_fetchrow($result))
			{
				$id = $row['id'];
				$uid = $row['user_id'];
				$name = $row['kingdom_name'];
				$name = '<a href="' . $phpbb_root_path . 'memberlist.php?mode=viewprofile&u='.$uid.'" class="ntext">'.$name.'</a>';
				$amount = $row['amount'];
			
				$template->assign_block_vars('rankrow', array(
					'RANK'		=>	$i + ($start + 1),
					'KINGDOMS'	=>	$name,
					'TOTAL'		=>	number_format($amount),
					)
				);	
				
				++$i;
			}	
			$db->sql_freeresult($result);
				
		}else{
			
			$sql = "SELECT *
			FROM " . AOD_KINGDOMS_TABLE . "
			ORDER BY kingdom_total_land DESC";			

			$result = $db->sql_query_limit($sql, $ranks_per_page, $start);
			while ($row = $db->sql_fetchrow($result))
			{
				$id = $row['id'];
				$uid = $row['user_id'];
				$name = $row['kingdom_name'];
				$name = '<a href="' . $phpbb_root_path . 'games/aod/viewking.php?uid='.$uid.'" class="ntext">'.$name.'</a>';
				$amount = $row['kingdom_total_land'];
			
				$template->assign_block_vars('rankrow', array(
					'RANK'		=>	$i + ($start + 1),
					'KINGDOMS'	=>	$name,
					'TOTAL'		=>	number_format($amount),
					)
				);	
				
				++$i;
			}	
			$db->sql_freeresult($result);		}		
	break;
	
	case 2: // Population
	
		$mode_display = sprintF($user->lang['RANKS_POP'],$ranks_per_page);
		$rank_display = $user->lang['POPULATION'];

		if($round > 1)
		{		
			$sql = "SELECT *
			FROM " . AOD_POP_RANKS_TABLE . "
			WHERE round = $round
			ORDER BY amount DESC";	

			$result = $db->sql_query_limit($sql, $ranks_per_page, $start);
			while ($row = $db->sql_fetchrow($result))
			{
				$id = $row['id'];
				$uid = $row['user_id'];
				$name = $row['kingdom_name'];
				$name = '<a href="' . $phpbb_root_path . 'memberlist.php?mode=viewprofile&u='.$uid.'" class="ntext">'.$name.'</a>';
				$amount = $row['amount'];
			
				$template->assign_block_vars('rankrow', array(
					'RANK'		=>	$i + ($start + 1),
					'KINGDOMS'	=>	$name,
					'TOTAL'		=>	number_format($amount),
					)
				);	
				
				++$i;
			}	
			$db->sql_freeresult($result);

		}else{
			
			$sql = "SELECT *
			FROM " . AOD_KINGDOMS_TABLE . "
			ORDER BY kingdom_pop DESC";	

			$result = $db->sql_query_limit($sql, $ranks_per_page, $start);
			while ($row = $db->sql_fetchrow($result))
			{
				$id = $row['id'];
				$uid = $row['user_id'];
				$name = $row['kingdom_name'];
				$name = '<a href="' . $phpbb_root_path . 'games/aod/viewking.php?uid='.$uid.'" class="ntext">'.$name.'</a>';
				$amount = $row['kingdom_pop'];
			
				$template->assign_block_vars('rankrow', array(
					'RANK'		=>	$i + ($start + 1),
					'KINGDOMS'	=>	$name,
					'TOTAL'		=>	number_format($amount),
					)
				);	
				
				++$i;
			}	
			$db->sql_freeresult($result);
			
		}		
	break;
	
	case 3: // Wins
	
		$mode_display = sprintF($user->lang['RANKS_WINS'],$ranks_per_page);
		$rank_display = $user->lang['WINS'];
		
		if($round > 1)
		{		
			$sql = "SELECT *
			FROM " . AOD_WINS_RANKS_TABLE . "
			WHERE round = $round
			ORDER BY amount DESC";	

			$result = $db->sql_query_limit($sql, $ranks_per_page, $start);
			while ($row = $db->sql_fetchrow($result))
			{
				$id = $row['id'];
				$uid = $row['user_id'];
				$name = $row['kingdom_name'];
				$name = '<a href="' . $phpbb_root_path . 'memberlist.php?mode=viewprofile&u='.$uid.'" class="ntext">'.$name.'</a>';
				$amount = $row['amount'];
			
				$template->assign_block_vars('rankrow', array(
					'RANK'		=>	$i + ($start + 1),
					'KINGDOMS'	=>	$name,
					'TOTAL'		=>	number_format($amount),
					)
				);	
				
				++$i;
			}	
			$db->sql_freeresult($result);

		}else{
			
			$sql = "SELECT *
			FROM " . AOD_KINGDOMS_TABLE . "
			ORDER BY kingdom_wins DESC";			


			$result = $db->sql_query_limit($sql, $ranks_per_page, $start);
			while ($row = $db->sql_fetchrow($result))
			{
				$id = $row['id'];
				$uid = $row['user_id'];
				$name = $row['kingdom_name'];
				$name = '<a href="' . $phpbb_root_path . 'games/aod/viewking.php?uid='.$uid.'" class="ntext">'.$name.'</a>';
				$amount = $row['kingdom_wins'];
			
				$template->assign_block_vars('rankrow', array(
					'RANK'		=>	$i + ($start + 1),
					'KINGDOMS'	=>	$name,
					'TOTAL'		=>	number_format($amount),
					)
				);	
				
				++$i;
			}	
			$db->sql_freeresult($result);
			
		}							
	break;
	
	default: // Networth
	
		$mode_display = sprintF($user->lang['RANKS_NET'],$ranks_per_page);
		$rank_display = $user->lang['NETWORTH'];
		
		if($round > 1)
		{		
			$sql = "SELECT *
			FROM " . AOD_NET_RANKS_TABLE . "
			WHERE round = $round
			ORDER BY amount DESC";	

			$result = $db->sql_query_limit($sql, $ranks_per_page, $start);
			while ($row = $db->sql_fetchrow($result))
			{
				$id = $row['id'];
				$uid = $row['user_id'];
				$name = $row['kingdom_name'];
				$name = '<a href="' . $phpbb_root_path . 'memberlist.php?mode=viewprofile&u='.$uid.'" class="ntext">'.$name.'</a>';
				$amount = $row['amount'];
			
				$template->assign_block_vars('rankrow', array(
					'RANK'		=>	$i + ($start + 1),
					'KINGDOMS'	=>	$name,
					'TOTAL'		=>	number_format($amount),
					)
				);	
				
				++$i;
			}	
			$db->sql_freeresult($result);

		}else{
			
			$sql = "SELECT *
			FROM " . AOD_KINGDOMS_TABLE . "
			ORDER BY kingdom_net DESC";			

			$result = $db->sql_query_limit($sql, $ranks_per_page, $start);
			while ($row = $db->sql_fetchrow($result))
			{
				$id = $row['id'];
				$uid = $row['user_id'];
				$name = $row['kingdom_name'];
				$name = '<a href="' . $phpbb_root_path . 'games/aod/viewking.php?uid='.$uid.'" class="ntext">'.$name.'</a>';
				$amount = $row['kingdom_net'];
			
				$template->assign_block_vars('rankrow', array(
					'RANK'		=>	$i + ($start + 1),
					'KINGDOMS'	=>	$name,
					'TOTAL'		=>	number_format($amount),
					)
				);	
				
				++$i;
			}	
			$db->sql_freeresult($result);

		}
	break;		
}

	
$archive_start = ($game_round - 1);
$archive_end = ($game_round - 25);
 	
for ($i = $archive_start; $i >= $archive_end; --$i)
{
	if($i > 0)
	{
		$template->assign_block_vars('archiverow', array(
			'ROUND'		=> $i,
			)
		);	
	}
}

// Only show rank archives if we are past round 1
if($game_round > 1)
{
	$show_archive = TRUE;
}

// Display an error messages to the user
$error_msgs = error_msgs($error_msgs);

$template->assign_vars(array(

	'MODE'					=> $mode_display,
	'RANK'					=> $rank_display,
	'PAGINATION'			=>	'<span class="stext">'.generate_pagination($pagination_url, $total_kingdoms, $ranks_per_page, $start).'</span>',
	'PAGE_NUMBER'			=>	on_page($total_kingdoms, $ranks_per_page, $start),
	'ROUND'					=> $round,
	'S_SHOW_ARCHIVE' 		=> $show_archive,

	)
);

include($phpbb_root_path . 'games/aod/includes/page_header.' . $phpEx);

// Output page
page_header($game_name.' - '.$user->lang['RANKING']);

$template->set_filenames(array(
	'body' => $templates_path.'rank_body.html')
);

page_footer();
?>